package com.xiaoyu.izf.service;

import cn.hutool.core.convert.Convert;
import com.google.common.collect.Lists;
import com.xiaoyu.izf.model.MerchantPayChannel;
import com.xiaoyu.izf.model.MerchantWxPayChannelInfo;
import com.xiaoyu.izf.model.MerchantZfbPayChannelInfo;
import com.xiaoyu.izf.model.UserInfo;
import com.xiaoyu.izf.model.page.DataTableBean;
import com.xiaoyu.izf.model.page.ParamListBean;
import com.xiaoyu.izf.repository.MerchantPayChannelDao;
import com.xiaoyu.izf.repository.MerchantWxPayChannelDao;
import com.xiaoyu.izf.repository.MerchantZfbPayChannelDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MerchantPayChannelService extends BaseService {

    private static Map<Integer,Class> map = new HashMap<>();

    static {
        map.put(1, null);
        map.put(2, null);
    }

    @Autowired
    private MerchantPayChannelDao merchantPayChannelDao;
    @Autowired
    private MerchantZfbPayChannelDao merchantZfbPayChannelDao;
    @Autowired
    private MerchantWxPayChannelDao merchantWxPayChannelDao;

    @Transactional
    public void add(int merchantId){
        for (Map.Entry<Integer,Class> entry :map.entrySet()){
            Integer type = entry.getKey();

            int associated = 0;
            MerchantZfbPayChannelInfo zfbPayChannelInfo = new MerchantZfbPayChannelInfo();
            MerchantWxPayChannelInfo wxPayChannelInfo = new MerchantWxPayChannelInfo();
            if (type==1){
                zfbPayChannelInfo.setInsertTime(new Timestamp(System.currentTimeMillis()));
                zfbPayChannelInfo =merchantZfbPayChannelDao.save(zfbPayChannelInfo);
                associated = zfbPayChannelInfo.getId();
            }else if (type==2){
                wxPayChannelInfo.setInsertTime(new Timestamp(System.currentTimeMillis()));
                wxPayChannelInfo = merchantWxPayChannelDao.save(wxPayChannelInfo);
                associated = wxPayChannelInfo.getId();
            }

            MerchantPayChannel channel = new MerchantPayChannel();
            channel.setAssociatedId(associated);
            channel.setInsertTime(new Timestamp(System.currentTimeMillis()));
            channel.setMerchantId(merchantId);
            channel.setOpened(0);
            channel.setType(type);
            channel.setTypeTips(type==1?"支付宝官方":"微信官方");
            channel.setIsUsing(0);
            channel.setSettleType(0);
            merchantPayChannelDao.save(channel);
        }
    }

    public DataTableBean listByMerchantId(ParamListBean param){
        PageRequest pageRequest = buildPageRequestSort(param.getPageNo(), param.getPageLength(), "id",param.getFieldName(),param.getCompositorType());
		Page<MerchantPayChannel> page = merchantPayChannelDao.findByMerchantId(Convert.toInt(param.getParam().get("merchantId")),pageRequest);
        return dataTableBean(page,param);
    }

    public MerchantPayChannel findOneById(int id){
        return merchantPayChannelDao.findOneById(id);
    }

    @Transactional
    public void setOpened(MerchantPayChannel payChannel,int opened){
        payChannel.setOpened(opened);
        merchantPayChannelDao.save(payChannel);
    }
}
